import axios from "axios";
import _ from "lodash";


async function getMgHotPlayList(f){


    await axios.get('/miguapi/recommend/playlist?type=2').then((r) => {

        let result = []
        for (let playlist of r.data.data.list) {
            result.push(dealmgResult(playlist))
        }

        axios.get('/miguapi/recommend/playlist?pageNo=2&type=2').then((r) => {
            for (let playlist of r.data.data.list) {
                result.push(dealmgResult(playlist))
            }
            axios.get('/miguapi/recommend/playlist?pageNo=3&type=2').then((r) => {
                for (let playlist of r.data.data.list) {
                    result.push(dealmgResult(playlist))
                }
                f(result)
            })
        })
    })
}
function dealmgResult(data){
    let {name:name,id:id,picUrl:imgUrl,creator :{ name: auth}} = data;


    return {
        "name":name,
        "id":id,
        "imgUrl":imgUrl,
        "auth":auth,
        "from":'migu'
    }
}
async function getMgPlayListDetail(id,f){
    await axios.get('/miguapi/playlist?id='+id).then((r) => {
        let result = dealDetailResult(r.data.data)
        f(result)
    }).catch((r) => {
        //console.log(r)
    })
}
function dealDetailResult(data){
 
    let {picUrl:img,playCount:playCount,name:name,id:id,desc:description} = data;
    let subscribedCount = ""

    let tags = data.tagLists.map((i) => {
        return i.tagName
    })
    let tracks = data.list;

    let r = tracks.map((i) => {
        let ts = i.duration;

        let min = Math.floor(ts/60);
        let sec = Math.floor(ts - min*60);
        if(min < 10){
            min = '0' + min;
        }
        if(sec < 10){
            sec = '0' + sec;
        }

        let al = i.album ?i.album.name:""
        return{
            'times' : `${min} : ${sec}`,
            'name' : i.name,
            'singer' : i.artists[0].name,
            'album' : al,
            'id' : i.cid
        }
    })


    return {
        user:"",
        tags:tags,
        name:name,
        id:id,
        description:description,
        img:img,
        playCount:playCount,
        subscribedCount:subscribedCount,
        list:r
    }
}


async function getMgPlayMusic(id,f){
    await axios.get('/miguapi/song?cid='+id).then((r) => {
        
        let result = dealPlayMusicResult(r.data)
        f(result)
    })
}
/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */
function dealPlayMusicResult(data){
    let resource =  "";
    if(data.data.flac){
        resource = data.data.flac;
    }else if(data.data[128]){
        resource = data.data[128];
    }else if(data.data[320]){
        resource = data.data[320];
    }
    if(resource === ""){
        return -1;
    }
    return {
        img:data.data.picUrl,
        resource:resource,

    }
}

async function getMgSearch(keywords,pageNo,pageSize,type,f){
    await axios.get(`/miguapi/search?keyword=${keywords}&type=${type}&pageNo=${pageNo}&pageSize=${pageSize}`).then((r) => {
        let result;
        if(type === 'song'){
            result = dealSearchSongResult(r.data.data)
            let count = r.data.data?.total || 0;
            result = {v:result,count:count}
        }else if(type === 'singer'){
            result = dealSearchSingerResult(r.data.data)
        }else if(type === 'playlist'){
            result = dealSearchPlayListResult(r.data.data)
        }else if(type === 'album'){
            result = dealSearchAlbumResult(r.data.data)
        }
        

        f(result)
    })
}
function dealSearchAlbumResult(data){
    let als = data.list || []
    return als.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "img":r.picUrl,
            "auth":r.artists[0].name,
            "from":'migu'
        }

    })



}
function dealSearchPlayListResult(data){
    let als = data.list || []
    return als.map((r) => {

        return {
            "name":r.name,
            "id":r.id,
            "imgUrl":r.picUrl,
            "auth":r.creator.name,
            "from":'migu'
        }

    })

}
function dealSearchSongResult(data) {
    let tracks = data.list;

    let r = tracks.map((i) => {

        let al = i.album ?i.album.name:""
        return{
            'times' : `00 : 00`,
            'name' : i.name,
            'singer' : i.artists[0].name,
            'album' : al,
            'id' : i.cid,
            'from' : 'migu'
        }
    })
    return r
}
function dealSearchSingerResult(data) {
    let result = data.list || {artistCount:0,artists:[{}]}
    let map = result.map((r) => {

        return {
            id:r.id,
            name: r.name,
            img: r.picUrl,
            from:'migu'
        }
    });
    return map
}

async function getMgSingerDetail(id,f){
    let total = 1;
    let page = 1;
    let result = [];
    do {
        await axios.get(`/miguapi/singer/albums?id=${id}&pageNo=${page}`).then((r) => {
            total = r.data.data.totalPage || 1;
            let t = dealMgSingerDetailResult(r.data.data)
            result = _.concat(result,t)
        })
        page++;

    }while (page <= total)
    f(result)
}

function dealMgSingerDetailResult(data) {
    let als = data.list || []

    let result = als.map((r) => {
        return{
            img:r.picUrl,
            id:r.id,
            name:r.name,
            auth:r.artists[0].name,
            from:'migu'
        }
    });

    return result;



}

async function getMgAlbumDetail(id,f){
    await axios.get('/miguapi/album/songs?id='+id).then((r) => {
        let result = dealAlbumDetailResult(r.data)
        f(result)
    })
}
function dealAlbumDetailResult(data){

    let tracks = data.data || [];

    let r = tracks.map((i) => {


        let al = i.album ?i.album.name:""
        return{
            'times' : `00 : 00`,
            'name' : i.name,
            'singer' : i.artists[0].name,
            'album' : al,
            'id' : i.cid
        }
    })


    return r
}

async function getMglyric(id,f){
    await axios.get(`/miguapi/lyric?cid=${id}`).then((r) => {
        //console.log(r.data)
        let result = deallyricResult(r.data)
        f(result.replaceAll('\r',''))
    })
}
function deallyricResult(data){

    return data.data || ''

}
export {getMgHotPlayList,getMgPlayListDetail,getMgPlayMusic,getMgSearch,getMgSingerDetail,getMgAlbumDetail,getMglyric}